﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using WMSSoft.Lib.Basic;
using WMSSoft.Lib.Enums;
using WMSSoft.Lib.WinKStart;
using WMSSoft.Lib.Texte;
using WMSSoft.Lib.WinKStart.Model;
using WMSSoft.Lib.WinKStart.ViewModel;
using WMSSoft.Lib.WinKStart.Extension;
using WMSSoft.Lib.WinKStart.Abstract;
using WMSSoft.Lib.WinKStart.Model.MenuConfig;
using WMSSoft.Lib.WinKStart.Classes.Metadata;
using System.Reflection;

namespace WMSSoft.WinKStart
{
    public class Setting_AddActionConditionViewModel : CCommandViewModel
    {
        ConditionItem _Data = null;
        List<KeyValueAssignment> _GUIElements = null;
        List<KeyValueAssignment> _ElementProperties = null;
        List<KeyValueAssignment> _Operators = null;
        List<KeyValueAssignment> _LogicalOperators = null;
        string _SelectedGUIElement = "";
        string _SelectedPropertyName = "";
        string _SelectedOperator = "";
        string _SelectedLogicalOperator = "";
        string _ConditionValue = "";
        int _ConditionValueLength = 0;

        public Setting_AddActionConditionViewModel()
        {
            PropertyChanged += Setting_AddActionConditionViewModel_PropertyChanged;
            base.ViewName = LanguageStrings.GetText("Add new Condition");
            _Data = new ConditionItem();

            _GUIElements = Meta_Elements.GetGUIElements("PROPERTIES");
            _ElementProperties = new List<KeyValueAssignment>();
            _ElementProperties.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose an GUI Element")));

            _Operators = Meta_Operator.GetPropertyOperators();
            _LogicalOperators = Meta_Operator.LogicalOperators();

            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.Cmd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.Cmd_OkDialog())));
        }



        public Setting_AddActionConditionViewModel(ConditionItem Condition)
        {
            PropertyChanged += Setting_AddActionConditionViewModel_PropertyChanged;
            base.ViewName = LanguageStrings.GetText("Edit Condition");
            _Data = Condition;

            _GUIElements = Meta_Elements.GetGUIElements("PROPERTIES");
            _ElementProperties = new List<KeyValueAssignment>();
            _ElementProperties.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose an GUI Element")));

            _Operators = Meta_Operator.GetPropertyOperators();
            _LogicalOperators = Meta_Operator.LogicalOperators();

            SelectedGUIElement = _Data.Element;
            SelectedPropertyName = _Data.Property;
            SelectedOperator = _Data.Operator;
            SelectedLogicalOperator = _Data.OperatorNextCondition;
            ConditionValue = _Data.ConditionValue;
            _ConditionValueLength = _Data.ValueLength;



            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Close"), new CDelegatingCommand(Delegate => this.Cmd_DiscardChanges())));
            m_oCommands.Add(new CSingleCommandViewModel(LanguageStrings.GetText("Ok"), new CDelegatingCommand(Delegate => this.Cmd_OkDialog())));
        }

        void Setting_AddActionConditionViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "SelectedGUIElement")
            {
                //TODO: Change selectable properties
                if (SelectedGUIElement != null && SelectedGUIElement.Length > 0)
                {
                    List<KeyValueAssignment> tmpElementProperties = Meta_Elements.GetGUIElementProperties(SelectedGUIElement);
                    ElementProperties = null;
                    if (tmpElementProperties != null && tmpElementProperties.Count() > 0)
                        ElementProperties = tmpElementProperties;
                    else
                    {
                        ElementProperties = new List<KeyValueAssignment>();
                        ElementProperties.Add(new KeyValueAssignment("", LanguageStrings.GetText("Please choose an GUI Element")));
                    }

                    SelectedPropertyName = "";
                }
            }
        }

        #region Properties

        public CSingleCommandViewModel Cmd_Ok
        {
            get { return m_oCommands[1]; }
        }

        public CSingleCommandViewModel Cmd_Close
        {
            get { return m_oCommands[0]; }
        }

        public ConditionItem Data
        {
            get { return _Data; }
        }

        public List<KeyValueAssignment> GUIElements
        {
            get { return _GUIElements; }
            set
            {
                _GUIElements = value;
                OnPropertyChanged("GUIElements");
            }
        }

        public List<KeyValueAssignment> ElementProperties
        {
            get { return _ElementProperties; }
            set
            {
                _ElementProperties = value;
                OnPropertyChanged("ElementProperties");
            }
        }

        public List<KeyValueAssignment> Operators
        {
            get { return _Operators; }
            set
            {
                _Operators = value;
                OnPropertyChanged("Operators");
            }
        }

        public List<KeyValueAssignment> LogicalOperators
        {
            get { return _LogicalOperators; }
            set
            {
                _LogicalOperators = value;
                OnPropertyChanged("LogicalOperators");
            }
        }

        public string SelectedGUIElement
        {
            get { return _SelectedGUIElement; }
            set
            {
                _SelectedGUIElement = value;
                OnPropertyChanged("SelectedGUIElement");
            }
        }

        public string SelectedPropertyName
        {
            get { return _SelectedPropertyName; }
            set
            {
                _SelectedPropertyName = value;
                OnPropertyChanged("SelectedPropertyName");
            }
        }

        public string SelectedOperator
        {
            get { return _SelectedOperator; }
            set
            {
                _SelectedOperator = value;
                OnPropertyChanged("SelectedOperator");
            }
        }

        public string SelectedLogicalOperator
        {
            get { return _SelectedLogicalOperator; }
            set
            {
                _SelectedLogicalOperator = value;
                OnPropertyChanged("SelectedLogicalOperator");
            }
        }

        public string ConditionValue
        {
            get { return _ConditionValue; }
            set
            {
                _ConditionValue = value;
                OnPropertyChanged("ConditionValue");
            }
        }

        public string ValueLength
        {
            get { return Convert.ToString(_ConditionValueLength); }
            set
            {
                try
                {
                    _ConditionValueLength = Convert.ToInt32(value);
                }
                catch
                {
                    _ConditionValueLength = 0;
                }
                OnPropertyChanged("ValueLength");
            }
        }

        #endregion

        #region Command Procedures

        void Cmd_DiscardChanges()
        {
            try
            {
                Setting_AddActionCondition parentDlg = null;
                if ((Setting_AddActionCondition)((CDelegatingCommand)Cmd_Close.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionCondition)((CDelegatingCommand)Cmd_Close.Command).CommandParameter;

                    if (parentDlg != null)
                        parentDlg.Close();
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't Close Dialog!"), Ex, EMessageTypes.Error);
            }
        }

        void Cmd_OkDialog()
        {
            try
            {
                Setting_AddActionCondition parentDlg = null;
                if ((Setting_AddActionCondition)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter != null)
                {
                    parentDlg = (Setting_AddActionCondition)((CDelegatingCommand)Cmd_Ok.Command).CommandParameter;

                    if (parentDlg != null)
                    {
                        if (ValidateSettings() == true)
                        {
                            _Data.Element = _SelectedGUIElement;
                            _Data.Property = _SelectedPropertyName;
                            _Data.Operator = _SelectedOperator;
                            _Data.OperatorNextCondition = _SelectedLogicalOperator;
                            _Data.ConditionValue = _ConditionValue;
                            _Data.ValueLength = _ConditionValueLength;
                            parentDlg.DialogResult = true;
                            parentDlg.Close();
                        }
                        else
                        {
                            System.Windows.MessageBox.Show(LanguageStrings.GetText("Condition can't add! Please validate the given Fields!"), 
                                                           LanguageStrings.GetText("Hint"));
                        }
                    }
                }
            }
            catch (Exception Ex)
            {
                CMessagelogging.AppendMessage(LanguageStrings.GetText("Can't apply Source-Element!"), Ex, EMessageTypes.Error);
            }
        }

        #endregion

        #region Helpers

        bool ValidateSettings()
        {
            try
            {
                if (_SelectedGUIElement != "" && _SelectedPropertyName != "" && _SelectedOperator != "" &&
                    _SelectedLogicalOperator != "" && ((_ConditionValueLength == 0 && _ConditionValue != "") |
                    (_ConditionValueLength > 0)))
                {
                    return true;
                }
                else return false;
            }
            catch (Exception Ex)
            {
                throw new Exception(LanguageStrings.GetText("Error on validating input data!"), Ex);                
            }
        }

        #endregion
    }
}
